var pool = require("./config/db.js"),
    markdown = require('markdown').markdown;

/**
 * 评级
 * @param ratings
 * @constructor
 */
function Ratings(ratings){
    if(ratings){
        this.hotel_id=ratings.hotel_id;
        this.rating=ratings.rating;
        this.totalvotes=ratings.totalvotes;
    }
};

module.exports = Ratings;

/**
 * 增
 * @param callback
 */
Ratings.prototype.save = function(callback) {
    var _obj = {
      rating:this.rating,
      totalvotes:this.totalvotes
    };
    pool.getConnection(function(err,connection){
        connection.query('INSERT INTO `ratings` SET ?',
            _obj,
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 删一个
 * @param callback
 */
Ratings.prototype.delete = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('DELETE FROM `ratings` WHERE hotel_id=?',
            [_id],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 改一个
 * @param callback
 */
Ratings.prototype.update = function(callback) {
    var _obj = {
        hotel_id:this.hotel_id,
        rating:this.rating,
        totalvotes:this.totalvotes
    };
    pool.getConnection(function(err,connection){
        connection.query('UPDATE `ratings` SET ? WHERE hotel_id =?',
            [
                _obj,
                _obj.hotel_id
            ],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 查一个
 * @param callback
 */
Ratings.prototype.getOne = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('SELECT * from `ratings` WHERE hotel_id=?',
            [_id]
            , function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 分页查
 * @param pagesize 每页个数
 * @param pages    当前页
 * @param callback
 */
Ratings.getTen = function(pagesize,pages,callback) {
    var _size = pagesize ? pagesize  : 10,
        _page = pages ? pages : 1,
        _total = null,
        _totalPage=null;

    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        if(_total == null){
            connection.query('SELECT COUNT(*) FROM `ratings`',function(err,rows,fields){
                _total = rows[0]['COUNT(*)'];
                _totalPage = _total / _size;
                _query();
            });
        }
        var _query = function(){
            connection.query('SELECT * from `ratings` ORDER BY `hotel_id` DESC LIMIT ?,?',
                [_totalPage>=1 ? (_page-1)*_size : 0,
                    _totalPage <_page ? (_page*_size) :_size]
                , function (err, rows, fields) {
                    if (err){
                        callback("查询出错");
                    }
                    connection.release();
                    callback(null,rows,_totalPage);
                });
        }
    });
};
